import {getToken,setToken,removeToken} from '@/tool/accessSession';

export default {
    state:{
        UserName        :'',//用户名
        NotLoginName    :'',//未登录前访问的路由
        UserSinginTime  :'',//登录时间戳
        UserAuthLevel   :'',//用户权限等级
        RouteName       :'',//路由名称
        RoutePath       :'',//路由路径
        TokenKey        :getToken(),//TokenKey
    },
    mutations: {
        /**
        * 登录成功
        * @param {Object} state
        * @param {Object} user
        */
        M_USER_SIGNIN(state, payload) {
            state.UserName          = payload;
            sessionStorage.user     = payload;
            sessionStorage.UserSinginTime = state.UserSinginTime = Date.parse(new Date()) /1000;
        },
        /**
        * 退出登录
        * @param {Object} state
        */
        M_USER_SIGNOUT(state) {

            for(let i in state){
                state[i] = '';
            }
            sessionStorage.clear();
        },
        /**
        *存储未登录之前需要访问的路由名
        *
        */
        M_USER_NOT_LOGIN_NAME(state,payload){
            state.NotLoginName = payload;
        },
        /*
        *存储tokenkey
        *
        */
        M_SET_TOKENKEY(state,payload){
            setToken(payload);
            state.TokenKey = payload;
        },
        /*
        * 路由名称
        */
        M_ROUT_ENAME(state,payload){
            state.RouteName = payload;
        },
        /*
        * 路由路径
        */
        M_ROUT_PATH(state,payload){
            state.RoutePath = payload;
        },
        /*
        *用户权限等级
        */
        M_USER_AUTH_LEVEL(state,payload){
            sessionStorage.UserAuthLevel = state.UserAuthLevel = payload;
        },

    },
    actions: {
        /**
        *用户登录
        */
        A_USER_SIGNIN({commit}, payload) {
            commit('M_USER_SIGNIN', payload);
        },
        /**
        *用户退出
        */
        A_USER_SIGNOUT({commit}) {
            commit('M_USER_SIGNOUT');
        },
        /**
        *登录前需要访问的路由
        */
        A_USER_NOT_LOGIN_NAME({commit},payload){
            commit('M_USER_NOT_LOGIN_NAME',payload);
        },
        /*
        *存储tokenkey
        */
        A_SET_TOKENKEY({commit},payload){
            commit('M_SET_TOKENKEY',payload);
        },
        /*
        *路由名称
        */
        A_ROUTER_NAME({commit},payload){
            commit('M_ROUT_ENAME',payload);
        },
        /*
        *路由路径
        */
        A_ROUTER_PATH({commit},payload){
            commit('M_ROUT_PATH',payload);
        },
        /*
        *用户权限等级
        */
        A_USER_AUTH_LEVEL({commit},payload){
            commit('M_USER_AUTH_LEVEL',payload);
        },
    },
    getters:{
        /*
        *获取用户是否登录和用户信息
        */
        G_USER_LOGIN_INFO(state){
            let user = {};
            if(state.UserName ||　sessionStorage.user){
                user.UserName   = state.UserName || sessionStorage.user;
                user.isLogin    = true;
            }else{
                user.UserName   = '';
                user.isLogin    = false;
            }
            return user;
        },
        /*
        *获取用户登录时间戳
        */
        G_USER_LOGIN_TIMES(state){
            return state.UserSinginTime || Number(sessionStorage.UserSinginTime);
        },
        /*
        *获取用户登录的tokenkey
        */
        G_TOKENKEY(state){
            return state.TokenKey;
        },
        /*
        *获取用户权限等级
        */
        G_USER_AUTH_LEVEL(state){
            return state.UserAuthLevel || Number(sessionStorage.UserAuthLevel);
        },
        /*
        *@param 默认第一个参数为当前state
        *@param 第二个函数也可接受其他getters作为参数
        *@param 在模块getter内，rootState根状态作为第三个参数
        */
        GET_ROUTER_NAME(state){
            return state.RouteName;
        },
        GET_ROUTER_PATH(state){
            return state.RoutePath;
        }
    }
}
